package org.example.sward.tooffer;

import java.util.Deque;
import java.util.LinkedList;

public class MaxSlidingWindow {

    public int[] maxSlidingWindow(int[] nums, int k) {
       if(nums.length == 0|| k==0){
           return new int[0];
       }
       Deque<Integer> deque = new LinkedList<>();
       int[] res = new int[nums.length - k + 1];
        for (int j = 0, i = 1 - k; j < nums.length; j++,i++) {
            if(i>0 && deque.peekFirst() == nums[i - 1]){
                deque.removeFirst();
            }
            while (!deque.isEmpty() && deque.peekLast() < nums[j]){
                deque.removeLast();
            }
            deque.addLast(nums[j]);
            if(i>0){
                res[i] = deque.peekFirst();
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,3,-1,-3,5,3,6,7};
        int k = 3;
        MaxSlidingWindow m = new MaxSlidingWindow();
       int[] result= m.maxSlidingWindow(nums,k);
        for (int i = 0; i < result.length ; i++) {
            System.out.println(result[i]);
        }
    }
}
